//logs.js
const loct = require('../../app.js')
const util = require('../../utils/util.js')
var QQMapWX = require('../../utils/qqmap-wx-jssdk1.2/qqmap-wx-jssdk')
var qqmapsdk = new QQMapWX({
  key: 'VYSBZ-K273P-GEQD3-LLP4A-KATX2-SIBXR' // 必填
})
Page({
  data: {
    src: '',
    logs: [],
    width: '',
    height: '',
    address: '123456',
    jinDu: '',
    weiDu: '',
    flash: 'off',
    sheT: 'back',
    shiJ: '',
    time: '',
    sf: '',
    nyr: ''

  },
  // "disableScroll": true禁止滑动
  onLoad: function (res) {
    // this.Gettime()
    console.log(this.data)
    // var jd = this.data.jinDu
    // var wd = this.data.weiDu
    var that = this
    wx.getLocation({
      type: "gcj02",
      success: res => {
        console.log(res)
        let jinDu = res.latitude //经度
        let weiDu = res.longitude //纬度
        this.setData({
          jinDu: jinDu,
          weiDu: weiDu
        })
        console.log(jinDu)
        console.log(weiDu)
        qqmapsdk.reverseGeocoder({
          location: {
            latitude: jinDu,
            longitude: weiDu
          },
          success: (res) => {
            that.setData({
              address: res.result.address
            })
            console.log(res)
            console.log(res.result.address);
          }
        })
      }
      //
    })
    this.Gettime() //调用时间函数
    //获取当前地理位置
  },
  getLocation(){
    let that = this;
    qqmapsdk.reverseGeocoder({
      location: {
        latitude: 39.984060,
        longitude: 116.307520
      },
      success: function(res) {
        that.setData({
          address: res.result.address
        })
        console.log(res)
        console.log(res.result.address);
      },
      fail:(res) =>{
        console.log(res)
      },
      complete: function(res) {
        console.log(res);
      }
    })
  },
  Gettime() {
    var hh = new Date();
    var y = hh.getFullYear(); //获取系统的年；
    var month = hh.getMonth() + 1; //获取系统月份，由于月份是从0开始计算，所以要加1
    // month = month <10? '0' + month : month;
    var ri = hh.getDate(); // 获取系统日，
    var h = hh.getHours();
    h = h < 10 ? '0' + h : h;
    var m = hh.getMinutes();
    m = m < 10 ? '0' + m : m;
    var d = hh.getUTCSeconds();
    d = d < 10 ? '0' + d : d;
    //获取星期几
    var day = hh.getDay();
    var weeks = new Array("周日", "周一", "周二", "周三", "周四", "周五", "周六");
    var week = weeks[day];
    var nyr = y + '年' + month + '月' + ri + '日' + week
    var sf = h + ':' + m + ':' + d
    this.setData({
      nyr: nyr,
      sf: sf
    })
    var ti = setTimeout(this.Gettime, 1000)//定时器  this多用

    // y + '年' + month + '月' + ri + '日' + h + ':' + m + ':' + d + week

    // return y + '-' + month + '-' + ri + '-' + h + ':' + m + ':' + d + week;
    // console.log(week);

    // but.onclick = function () {
    //     clearTimeout(ti)
    // }
    // console.log(adr)
  },
  //闪光灯
  shanG() {
    let kais = this.data.flash
    switch (kais) {
      case 'off':
        this.setData({
          flash: 'torch',
        })
        break;
      case 'torch':
        this.setData({
          flash: 'off',
        })
        break;
      default:
        break;
    }

  },
  yuL() {
    wx.showModal({
      title: '提示',
      content: '请到手机相册查看',
      success(res) {
        if (res.confirm) {
          console.log('用户点击好的')
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  // 摄像头转换
  getSave() {
    let sheT = this.data.sheT //off   troch
    switch (sheT) {
      case 'back':
        this.setData({
          sheT: 'front'
        })
        break;
      case 'front':
        this.setData({
          sheT: 'back'
        })
        break;
      default:
        break;
    }
  },
  record() {
    // clearTimeout(ti)
    var adr = this.data.address
    var jidu = this.data.jinDu
    var wedu = this.data.weiDu
    var time = this.data.time
    var times = this.data.times
    var hh = new Date();
    var y = hh.getFullYear(); //获取系统的年；
    var month = hh.getMonth() + 1; //获取系统月份，由于月份是从0开始计算，所以要加1
    // // month = month <10? '0' + month : month;
    var ri = hh.getDate(); // 获取系统日，
    var h = hh.getHours();
    h = h < 10 ? '0' + h : h;
    var m = hh.getMinutes();
    m = m < 10 ? '0' + m : m;
    var d = hh.getUTCSeconds();
    d = d < 10 ? '0' + d : d;
    // //获取星期几
    var day = hh.getDay();
    var weeks = new Array("周日", "周一", "周二", "周三", "周四", "周五", "周六");
    var week = weeks[day];
    var time = y + '年' + month + '月' + ri + '日' + week
    var times = h + ':' + m
    this.setData({
      shiJ: times, //时分
      time: time, //  年月日周
      //  times:times
    })
    // y + '年' + month + '月' + ri + '日' + h + ':' + m + ':' + d + week
    console.log(time)
    // return y + '-' + month + '-' + ri + '-' + h + ':' + m + ':' + d + week;
    console.log(week);
    console.log(adr)
    var fg= wx.createCameraContext()
    fg.takePhoto({
      quality: 'high', //高质量的图片
      // device-position:'front',
      success: res => {
        //res.tempImagePath照片文件在手机内的的临时路径
        let tempImagePath = res.tempImagePath
        let w = res.width
        let h = res.height
        let t = 0.75
        this.setData({
          width: w,
          height: h
        })
        console.log(res.width)
        console.log(res.height)
        var that = this
        wx.saveFile({
          tempFilePath: tempImagePath,
          dd: w,
          hh: h,
          success: function (res) {
            //返回保存时的临时路径 res.savedFilePath
            const savedFilePath = res.savedFilePath;
            var ctx = wx.createCanvasContext('shareCanvas')
            ctx.drawImage(res.savedFilePath, 0, 0, w, h, 0, 0, w, h);
            ctx.setFontSize(20)
            ctx.fillText(jidu, w - 120, 40)
            ctx.fillText(wedu, w - 120, 75)
            ctx.setFontSize(85)
            ctx.setFillStyle('#F9D89A')
            ctx.fillText(times, 35, h - 85)
            ctx.setFontSize(40)
            ctx.fillText(time, 270, h - 85)
            // ctx.fillText(, 45, h - 20)
            ctx.drawImage("../images/dingwei1.png", 0, 0, 50, 50, 45, h - 60, 50, 50);
            ctx.fillText(adr, 82, h - 20)
            // ctx.setFontSize(20)
            // ctx.moveTo(10, 10)
            // ctx.rect(10, 10, 100, 50)
            // ctx.lineTo(110, 60)
            // ctx.stroke()
            // ctx.setFillStyle('red')
            // ctx.fillText('MINA', 1, 1)
            ctx.draw(true, function () {
              wx.canvasToTempFilePath({
                x: 0,
                y: 0,
                width: w,
                height: h,
                destWidth: w,
                destHeight: h,
                canvasId: 'shareCanvas',
                success(res) {
                  console.log(res.tempFilePath)
                  wx.saveImageToPhotosAlbum({
                    filePath: res.tempFilePath,
                  })
                }
              })
            })
            console.log(res.savedFilePath)
            that.setData({
              src: res.savedFilePath
            })
            // 保存到本地相册   自动保存
            // wx.saveImageToPhotosAlbum({
            //   filePath: res.savedFilePath,
            // })
          },
          //保存失败回调（比如内存不足）
          fail: console.log
        })
      },
    })

  }
})